package com.cn.yang.servlet.mainPage;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.model.mainPage.MainPageModel;
import com.cn.yang.service.MainPageService;
import com.cn.yang.service.impl.MainPageServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/mainPage/updatMainPage")
public class UpdateMainPageServlet extends HttpServlet {
    //查询数据，返回==============
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        Integer id = Integer.valueOf(req.getParameter("id"));
        MainPageService service = new MainPageServiceImpl();
        MainPageModel model = service.selectById(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mainPageModel",model);
        resp.getWriter().println(jsonObject);
    }
    // 更新数据库===========================================================
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        Integer id = Integer.valueOf(req.getParameter("id"));
        String title = req.getParameter("title");
        String englishname = req.getParameter("englishname");
        String isshow = req.getParameter("isshow");

        MainPageModel model = new MainPageModel();
        model.setId(id);
        model.setTitle(title);
        model.setEnglishName(englishname);
        model.setIsshow(isshow);

        MainPageService service = new MainPageServiceImpl();
        int result = service.updateMainPage(model);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);

    }
}
